using System;
using System.Runtime.InteropServices;
using UnityEngine;

using MemoryPack;

namespace Voxels {


	public static class VisibilityFlagsHelper {
		public static bool IsSet(this VisibilityFlags flags, VisibilityFlags flag) {
			return (flags & flag) != 0;
		}

		public static void Set(ref VisibilityFlags flags, VisibilityFlags flag) {
			flags = (flags | flag);
		}

		public static void Unset(ref VisibilityFlags flags, VisibilityFlags flag) {
			flags = (flags & (~flag));
		}
	}

	public static class ChunkHelper {
		public static void Spiral(int dimX, int dimY, Action<int,int> callback) {
			int x, y, dx, dy;
			x = y = dx = 0;
			dy = -1;
			var t = Mathf.Max(dimX, dimY);
			var maxI = t * t;
			for ( int i = 0; i < maxI; i++ ) {
				if ( (-dimX / 2 <= x) && (x <= dimX / 2) && (-dimY / 2 <= y) && (y <= dimY / 2) ) {
					callback(x, y);
				}
				if ( (x == y) || ((x < 0) && (x == -y)) || ((x > 0) && (x == 1 - y)) ) {
					t = dx;
					dx = -dy;
					dy = t;
				}
				x += dx;
				y += dy;
			}
		}
	}
}
